package logic

import (
	"context"
	"doctor-medicine-system/model/mysql"
	"errors"
	"gorm.io/gorm"

	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *doctorrpc.UpdateRequest) (*doctorrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	medicine := mysql.Medicine{
		Model:   gorm.Model{ID: uint(in.MedicineId)},
		Name:    in.Name,
		Big:     in.Big,
		Address: in.Address,
		Shi:     in.Shi,
		Num:     int(in.Num),
		Zhu:     in.Zhu,
		Typer:   int(in.Typer),
	}
	err := medicine.UpdateMedicine(mysql.DB)
	if err != nil {
		return nil, err
	}
	var doctor mysql.Doctor
	err = doctor.FinfDoctor(mysql.DB, in.DoctorId)
	if err != nil {
		return nil, err
	}
	if doctor.Typer != "医生" {
		return nil, errors.New("请让医生修改")
	}
	return &doctorrpc.UpdateResponse{
		MedicineId: int64(medicine.ID),
	}, nil
}
